/**
 * 简洁的 API 模块
 */

export * from './types';
export { api } from './client';

import { api } from './client';
import type {
  AvatarGroup,
  Avatar,
  AvatarDetails,
  HeyGenAccount,
  CreateAccountRequest,
  LoginRequest,
  LoginData,
  UserInfo,
  DashboardStats,
  ListResponse,
  TextGenerationRequest,
  TextGenerationResponse,
  TextGenerationHistory,
  TextGenerationStats,
  TextTemplate,
  AIProvider,
  CreateAIProviderRequest,
  UpdateAIProviderRequest,
  UserPersonaAnalysisRequest,
  UserPersonaAnalysisResponse,
  UserPersonaHistory,
  UserPersonaStats,
  SocialAccount,
  CreateSocialAccountRequest,
  UpdateSocialAccountRequest,
  SocialAccountWithDetails,
  AccountMemory,
  UpdateAccountMemoryRequest,
  AccountPreferences,
  UpdateAccountPreferencesRequest,
  EnhancedTextGenerationRequest,
  SocialAccountStats,
} from './types';

// Avatar APIs - 默认使用缓存数据
export const avatarApi = {
  getGroups: (params?: { include_public?: boolean; limit?: number }) =>
    api.get<ListResponse<AvatarGroup>>('/avatar-groups', params),

  getGroupAvatars: (groupId: string, params?: { limit?: number }) =>
    api.get<ListResponse<Avatar>>(`/avatar-groups/${groupId}/avatars`, params),

  getDetails: (avatarId: string) => api.get<AvatarDetails>(`/avatars/${avatarId}`),
};

// HeyGen Account APIs
export const accountApi = {
  list: () => api.get<ListResponse<HeyGenAccount>>('/heygen-accounts'),

  create: (data: CreateAccountRequest) => api.post<HeyGenAccount>('/heygen-accounts', data),

  get: (id: number) => api.get<HeyGenAccount>(`/heygen-accounts/${id}`),

  activate: (id: number) => api.put<HeyGenAccount>(`/heygen-accounts/${id}/activate`),

  getActive: () => api.get<HeyGenAccount>('/heygen-accounts/active'),

  refreshQuota: (id: number) => api.put<HeyGenAccount>(`/heygen-accounts/${id}/refresh-quota`),

  delete: (id: number) => api.delete<void>(`/heygen-accounts/${id}`),
};

// Auth APIs
export const authApi = {
  login: (credentials: LoginRequest) => api.post<LoginData>('/auth/login', credentials),

  logout: () => api.post<void>('/auth/logout'),

  getProfile: () => api.get<UserInfo>('/auth/me'),
};

// Dashboard APIs (simplified)
export const dashboardApi = {
  getStats: () => api.get<DashboardStats>('/dashboard/stats'),
};

// Folder APIs
export const folderApi = {
  getFolders: () => api.get<Array<{ id: string; name: string; parent_id?: string }>>('/folders'),
  createFolder: (data: { name: string; parent_id?: string }) =>
    api.post<{ id: string; name: string; parent_id?: string }>('/folders', data),
};

// Text Generation APIs
export const textGenerationApi = {
  // 生成文案
  generate: (data: TextGenerationRequest) =>
    api.post<TextGenerationResponse>('/text-generation/generate', data),

  // 获取文案历史记录
  getHistory: (params?: { page?: number; limit?: number; type?: string }) =>
    api.get<ListResponse<TextGenerationHistory>>('/text-generation/history', params),

  // 获取单个历史记录
  getHistoryItem: (id: string) => api.get<TextGenerationHistory>(`/text-generation/history/${id}`),

  // 删除历史记录
  deleteHistory: (id: string) => api.delete<void>(`/text-generation/history/${id}`),

  // 收藏/取消收藏
  toggleFavorite: (id: string) =>
    api.put<TextGenerationHistory>(`/text-generation/history/${id}/favorite`),

  // 获取统计数据
  getStats: () => api.get<TextGenerationStats>('/text-generation/stats'),

  // 获取模板列表
  getTemplates: (params?: { type?: string; is_active?: boolean }) =>
    api.get<ListResponse<TextTemplate>>('/text-generation/templates', params),

  // 创建模板
  createTemplate: (data: Omit<TextTemplate, 'id'>) =>
    api.post<TextTemplate>('/text-generation/templates', data),

  // 更新模板
  updateTemplate: (id: string, data: Partial<TextTemplate>) =>
    api.put<TextTemplate>(`/text-generation/templates/${id}`, data),

  // 删除模板
  deleteTemplate: (id: string) => api.delete<void>(`/text-generation/templates/${id}`),

  // 获取指定提供商的可用模型
  getProviderModels: (providerId: number) =>
    api.get<string[]>(`/ai-providers/${providerId}/models`),
};

// AI Provider APIs
export const aiProviderApi = {
  // 创建AI服务提供商
  create: (data: CreateAIProviderRequest) => api.post<AIProvider>('/ai-providers', data),

  // 获取所有AI服务提供商
  list: () => api.get<ListResponse<AIProvider>>('/ai-providers'),

  // 获取特定AI服务提供商
  get: (id: number) => api.get<AIProvider>(`/ai-providers/${id}`),

  // 更新AI服务提供商
  update: (id: number, data: UpdateAIProviderRequest) =>
    api.put<AIProvider>(`/ai-providers/${id}`, data),

  // 删除AI服务提供商
  delete: (id: number) => api.delete<void>(`/ai-providers/${id}`),

  // 激活AI服务提供商
  activate: (id: number) => api.put<AIProvider>(`/ai-providers/${id}/activate`),

  // 刷新AI服务提供商信息
  refresh: (id: number) => api.put<AIProvider>(`/ai-providers/${id}/refresh`),

  // 获取活跃的AI服务提供商
  getActive: (type?: string) =>
    api.get<AIProvider>('/ai-providers/active', type ? { type } : undefined),

  // 获取提供商可用模型
  getProviders: () => api.get<ListResponse<AIProvider>>('/ai-providers'),

  // 获取指定提供商的可用模型
  getProviderModels: (providerId: number) =>
    api.get<string[]>(`/ai-providers/${providerId}/models`),
};

// User Persona Analysis APIs
export const userPersonaApi = {
  // 分析用户画像
  analyze: (data: UserPersonaAnalysisRequest) =>
    api.post<UserPersonaAnalysisResponse>('/user-persona/analyze', data),

  // 获取历史记录
  getHistory: (params?: string) =>
    api.get<ListResponse<UserPersonaHistory>>(`/user-persona/history${params || ''}`),

  // 获取单个历史记录
  getHistoryItem: (id: string) => api.get<UserPersonaHistory>(`/user-persona/history/${id}`),

  // 删除历史记录
  deleteHistory: (id: string) => api.delete<void>(`/user-persona/history/${id}`),

  // 收藏/取消收藏
  toggleFavorite: (id: string) =>
    api.put<UserPersonaHistory>(`/user-persona/history/${id}/favorite`),

  // 获取统计数据
  getStats: () => api.get<UserPersonaStats>('/user-persona/stats'),
};

// Social Account APIs - 自媒体账号管理
export const socialAccountApi = {
  // 获取账号列表
  list: () => api.get<ListResponse<SocialAccount>>('/social-accounts'),

  // 创建账号
  create: (data: CreateSocialAccountRequest) => api.post<SocialAccount>('/social-accounts', data),

  // 获取单个账号
  get: (id: string) => api.get<SocialAccountWithDetails>(`/social-accounts/${id}`),

  // 更新账号基本信息
  update: (id: string, data: UpdateSocialAccountRequest) =>
    api.put<SocialAccount>(`/social-accounts/${id}`, data),

  // 删除账号
  delete: (id: string) => api.delete<void>(`/social-accounts/${id}`),

  // 激活/切换账号
  activate: (id: string) => api.put<SocialAccount>(`/social-accounts/${id}/activate`),

  // 获取当前激活的账号
  getActive: () => api.get<SocialAccountWithDetails>('/social-accounts/active'),

  // 账号记忆管理
  memory: {
    // 获取账号记忆
    get: (accountId: string) => api.get<AccountMemory>(`/social-accounts/${accountId}/memory`),

    // 更新账号记忆
    update: (accountId: string, data: UpdateAccountMemoryRequest) =>
      api.put<AccountMemory>(`/social-accounts/${accountId}/memory`, data),

    // 重置账号记忆
    reset: (accountId: string) => api.delete<void>(`/social-accounts/${accountId}/memory`),
  },

  // 账号偏好设置
  preferences: {
    // 获取账号偏好
    get: (accountId: string) =>
      api.get<AccountPreferences>(`/social-accounts/${accountId}/preferences`),

    // 更新账号偏好
    update: (accountId: string, data: UpdateAccountPreferencesRequest) =>
      api.put<AccountPreferences>(`/social-accounts/${accountId}/preferences`, data),

    // 重置偏好设置
    reset: (accountId: string) => api.delete<void>(`/social-accounts/${accountId}/preferences`),
  },

  // 账号统计
  getStats: (accountId: string) =>
    api.get<SocialAccountStats>(`/social-accounts/${accountId}/stats`),

  // 获取所有账号的统计概览
  getOverviewStats: () =>
    api.get<{
      total_accounts: number;
      active_accounts: number;
      platform_distribution: Record<string, number>;
      content_by_platform: Record<string, number>;
    }>('/social-accounts/stats/overview'),
};

// Enhanced Text Generation APIs - 支持账号上下文的文案生成
export const enhancedTextGenerationApi = {
  // 使用账号上下文生成文案
  generateWithAccount: (data: EnhancedTextGenerationRequest) =>
    api.post<TextGenerationResponse>('/text-generation/generate-enhanced', data),

  // 获取账号相关的生成历史
  getAccountHistory: (
    accountId: string,
    params?: { page?: number; limit?: number; type?: string }
  ) =>
    api.get<ListResponse<TextGenerationHistory>>(
      `/social-accounts/${accountId}/content-history`,
      params
    ),

  // 学习账号内容表现
  recordContentPerformance: (
    accountId: string,
    contentId: string,
    metrics: {
      views?: number;
      likes?: number;
      shares?: number;
      comments?: number;
      user_feedback?: 'positive' | 'negative' | 'neutral';
    }
  ) =>
    api.post<void>(
      `/social-accounts/${accountId}/content-history/${contentId}/performance`,
      metrics
    ),
};
